<?php

App::uses('CrudController', 'Controller');
App::uses('Categorization', 'Model');

class CategorizationsController extends CrudController
{
    public $_model = 'Categorization';

    public function admin($lineId, $areaId, $moeId) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $this->loadModel('Moe');
        $this->loadModel('Line');
        $this->loadModel('Test');
        $appBosch = new stdClass();
        $tests = $this->Test->getEnabledByArea($areaId);
        $icons = array(
            array(
                'text' => 'fa-check',
                'value' => 'fa-check',
            )
        );
        $appBosch->tests = $tests;
        $appBosch->lines = $this->Line->getEnabledByArea($areaId);
        $appBosch->lineId = $lineId;
        $appBosch->areaId = $areaId;
        $appBosch->moeId = $moeId;
        $appBosch->icons = $icons;
        $description = $this->getDescription($lineId, $areaId, $moeId);
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Niveles'));
        $this->set('description', $description);
    }

    private function getDescription($lineId, $areaId, $moeId) {
        $this->Moe->id = $moeId;
        $moe = $this->Moe->read();
        $this->Area->id = $areaId;
        $area = $this->Area->read();
        $this->Line->id = $lineId;
        $line = $this->Line->read();
        $parts = array(
            $moe['Moe']['name'],
            $area['Area']['name'],
            $line['Line']['name'],
        );
        return implode(' - ', $parts);
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getEnabledByLine($param);
        array_walk($records, function(&$record) {
            $record['requires_approval'] = $record['requires_approval'] === '1';
            $record['test_id'] = $record['test_id'] === null ? 0 : $record['test_id'];
        });
        return $records;
    }

    protected function c($model) {
        return array(
            'test_id' => $this->getTest($model->test_id),
            'line_id' => trim($model->line_id),
            'name' => trim($model->name),
            'certifications' => (int) $model->certifications,
            'colour' => trim($model->colour),
            'icon' => trim($model->icon),
            'lower_limit' => (int) $model->lower_limit,
            'requires_approval' => $model->requires_approval === true ? Categorization::REQUIERES_APROVAL : Categorization::NO_REQUIERES_APROVAL,
            'status' => Categorization::STATUS_ENABLED,
        );
    }

    /**
     * Si el id del examen es igual a cero significa que ningun examen esta asociado a este nivel. SI es diferente a
     * cero entonces representa el id del examen requerido para este niel.
     * @param $testId
     * @return string UUID
     */
    private function getTest($testId)
    {
        return ((int)$testId) === 0 ? null : $testId;

    }

    protected function u($model) {
        return array(
            'test_id' => $this->getTest($model->test_id),
            'line_id' => $model->line_id,
            'name' => trim($model->name),
            'colour' => trim($model->colour),
            'icon' => trim($model->icon),
            'lower_limit' => (int) $model->lower_limit,
            'requires_approval' => $model->requires_approval === true ? Categorization::REQUIERES_APROVAL : Categorization::NO_REQUIERES_APROVAL,
            'certifications' => (int) $model->certifications,
        );
    }

}
